from django import template

register = template.Library()

from django.template.defaultfilters import stringfilter

# the filter fails when comes to processing strings 
# in other locales rather than Chinese characters.
# a Chinese character is twice as wide as an English charater.
# so this filter will be kept just for a demonstration of custom filter
# in django's templating system.
# see http://stackoverflow.com/questions/770854/django-custom-template-tags-in-google-app-engine
# and http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#howto-custom-template-tags
# for detail.
@register.filter
@stringfilter
def truncatezh(value, arg):
    try:
        length = int(arg)
    except:
        length = 0
    if length >0 and length <len(value):
        return "%s..." % value[:length-1]
    else:
        return value
